<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="操作类型">
              <j-dict-select-tag placeholder="请选择操作类型" v-model="queryParam.stoneActionType" dictCode="stone_action_type"/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="订单号">
              <a-input placeholder="请输入订单号" v-model="queryParam.orderCodeNum"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="成色">
                <j-search-select-tag placeholder="请选择成色" v-model="queryParam.karat" dict="gold_karat,gold_karat_name,id"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="种类">
                <j-dict-select-tag placeholder="请选择种类" v-model="queryParam.cate" dictCode="gold_type"/>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button type="primary" icon="usergroup-add" v-has="'stoneEntry:outin'" @click="opsByStep" >批量出入库操作</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
        <span style="float:right;">
          <a @click="loadData()"><a-icon type="sync" />刷新</a>
          <a-divider type="vertical" />
          <a-popover title="自定义列" trigger="click" placement="leftBottom">
            <template slot="content">
              <a-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
                <a-row style="width: 400px">
                  <template v-for="(item,index) in defColumns">
                    <template v-if="item.key!='rowIndex'&& item.dataIndex!='action'">
                        <a-col :span="12"><a-checkbox :value="item.dataIndex"><j-ellipsis :value="item.title" :length="10"></j-ellipsis></a-checkbox></a-col>
                    </template>
                  </template>
                </a-row>
              </a-checkbox-group>
            </template>
            <a><a-icon type="setting" />设置</a>
          </a-popover>
        </span>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)" v-has="'stoneEntry:edit'">修改</a>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item v-has="'stoneEntry:delete'">
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <stone-entry-modal ref="modalForm" @ok="modalFormOk"></stone-entry-modal>
    <step-modal ref="stepForm" @ok="loadData(1)"></step-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import StoneEntryModal from './modules/StoneEntryModal'
  import StepModal from './modules/StepModal'
  import Vue from 'vue'

  export default {
    name: 'StoneEntryList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      StoneEntryModal, StepModal
    },
    data () {
      return {
        description: '石出入库管理页面',
        // 表头
        columns: [
          {
            title: '序号',
            dataIndex: '',
            key:'rowIndex',
            width:40,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'类型',
            align:"center",
            sorter: true,
            dataIndex: 'stoneActionType_dictText'
          },
          {
            title:'采购单号',
            align:"center",
            sorter: true,
            dataIndex: 'purchaseOrderNum'
          },
          {
            title:'工单号',
            align:"center",
            sorter: true,
            dataIndex: 'workNum'
          },
          {
            title:'拆分页',
            align:"center",
            sorter: true,
            dataIndex: 'pn'
          },
          {
            title:'石号',
            align:"center",
            sorter: true,
            dataIndex: 'stoneNum'
          },
          {
            title:'石号名称',
            align:"center",
            sorter: true,
            dataIndex: 'stoneName'
          },
          {
            title:'石卡号',
            align:"center",
            sorter: true,
            dataIndex: 'codeNum'
          },
          {
            title:'石类',
            align:"center",
            sorter: true,
            dataIndex: 'cateId_dictText'
          },
          {
            title:'筛号',
            align:"center",
            sorter: true,
            dataIndex: 'sizeNum'
          },
          {
            title:'级别',
            align:"center",
            sorter: true,
            dataIndex: 'level_dictText'
          },
          {
            title:'形状',
            align:"center",
            sorter: true,
            dataIndex: 'shape_dictText'
          },
          {
            title:'规格',
            align:"center",
            sorter: true,
            dataIndex: 'style_dictText'
          },
          {
            title:'净度',
            align:"center",
            sorter: true,
            dataIndex: 'neatness_dictText'
          },
          {
            title:'车工',
            align:"center",
            sorter: true,
            dataIndex: 'cutting_dictText'
          },
          {
            title:'颜色',
            align:"center",
            sorter: true,
            dataIndex: 'color_dictText'
          },
          {
            title:'质地',
            align:"center",
            sorter: true,
            dataIndex: 'texture_dictText'
          },
          {
            title:'角度',
            align:"center",
            sorter: true,
            dataIndex: 'angle'
          },{
            title:'单粒重',
            align:"center",
            sorter: true,
            dataIndex: 'weight'
          },
          {
            title:'好石数',
            align:"center",
            sorter: true,
            dataIndex: 'goodStoneCount'
          },
          {
            title:'好石重',
            align:"center",
            sorter: true,
            dataIndex: 'goodStoneWeight'
          },
          {
            title:'坏石数',
            align:"center",
            sorter: true,
            dataIndex: 'badStoneCount'
          },
          {
            title:'坏石重',
            align:"center",
            sorter: true,
            dataIndex: 'badStoneWeight'
          },
          {
            title:'镶法',
            align:"center",
            sorter: true,
            dataIndex: 'inlay_dictText'
          },
          {
            title:'成本单价',
            align:"center",
            sorter: true,
            dataIndex: 'stoneUnitCost'
          },
          {
            title:'单价',
            align:"center",
            sorter: true,
            dataIndex: 'stoneUnitPrice'
          },
          {
            title:'批号',
            align:"center",
            sorter: true,
            dataIndex: 'batchNum'
          },
          {
            title:'来单号',
            align:"center",
            sorter: true,
            dataIndex: 'receiveOrderNum'
          },
          {
            title:'主副石',
            align:"center",
            sorter: true,
            dataIndex: 'mainStone_dictText'
          },
          {
            title:'发票类型',
            align:"center",
            sorter: true,
            dataIndex: 'invoiceType_dictText'
          },
          {
            title:'实际币种',
            align:"center",
            sorter: true,
            dataIndex: 'currencyType_dictText'
          },
          {
            title:'实际单价',
            align:"center",
            sorter: true,
            dataIndex: 'stoneUnitPlay'
          },
          {
            title:'实际金额',
            align:"center",
            sorter: true,
            dataIndex: 'stoneCost'
          },
          {
            title:'汇率',
            align:"center",
            sorter: true,
            dataIndex: 'exchangeRate'
          },
          {
            title:'计价方式',
            align:"center",
            sorter: true,
            dataIndex: 'countPricePattern_dictText'
          },
          {
            title:'备注',
            align:"center",
            dataIndex: 'remark'
          },
          {
            title:'发出方',
            align:"center",
            sorter: true,
            dataIndex: 'senderName'
          },
          {
            title:'发出时间',
            align:"center",
            sorter: true,
            dataIndex: 'sendTime'
          },
          {
            title:'接收方',
            align:"center",
            sorter: true,
            dataIndex: 'receiverName'
          },
          {
            title:'接收时间',
            align:"center",
            sorter: true,
            dataIndex: 'receiveTime'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        //列设置
        settingColumns:[],
        //列定义
        defColumns: [
          {
            title: '序号',
            dataIndex: '',
            key:'rowIndex',
            width:40,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'类型',
            align:"center",
            sorter: true,
            dataIndex: 'stoneActionType_dictText'
          },
          {
            title:'采购单号',
            align:"center",
            sorter: true,
            dataIndex: 'purchaseOrderNum'
          },
          {
            title:'工单号',
            align:"center",
            sorter: true,
            dataIndex: 'workNum'
          },
          {
            title:'拆分页',
            align:"center",
            sorter: true,
            dataIndex: 'pn'
          },
          {
            title:'石号',
            align:"center",
            sorter: true,
            dataIndex: 'stoneNum'
          },
          {
            title:'石号名称',
            align:"center",
            sorter: true,
            dataIndex: 'stoneName'
          },
          {
            title:'石卡号',
            align:"center",
            sorter: true,
            dataIndex: 'codeNum'
          },
          {
            title:'石类',
            align:"center",
            sorter: true,
            dataIndex: 'cateId_dictText'
          },
          {
            title:'筛号',
            align:"center",
            sorter: true,
            dataIndex: 'sizeNum'
          },
          {
            title:'级别',
            align:"center",
            sorter: true,
            dataIndex: 'level_dictText'
          },
          {
            title:'形状',
            align:"center",
            sorter: true,
            dataIndex: 'shape_dictText'
          },
          {
            title:'规格',
            align:"center",
            sorter: true,
            dataIndex: 'style_dictText'
          },
          {
            title:'净度',
            align:"center",
            sorter: true,
            dataIndex: 'neatness_dictText'
          },
          {
            title:'车工',
            align:"center",
            sorter: true,
            dataIndex: 'cutting_dictText'
          },
          {
            title:'颜色',
            align:"center",
            sorter: true,
            dataIndex: 'color_dictText'
          },
          {
            title:'质地',
            align:"center",
            sorter: true,
            dataIndex: 'texture_dictText'
          },
          {
            title:'角度',
            align:"center",
            sorter: true,
            dataIndex: 'angle'
          },{
            title:'单粒重',
            align:"center",
            sorter: true,
            dataIndex: 'weight'
          },
          {
            title:'好石数',
            align:"center",
            sorter: true,
            dataIndex: 'goodStoneCount'
          },
          {
            title:'好石重',
            align:"center",
            sorter: true,
            dataIndex: 'goodStoneWeight'
          },
          {
            title:'坏石数',
            align:"center",
            sorter: true,
            dataIndex: 'badStoneCount'
          },
          {
            title:'坏石重',
            align:"center",
            sorter: true,
            dataIndex: 'badStoneWeight'
          },
          {
            title:'镶法',
            align:"center",
            sorter: true,
            dataIndex: 'inlay_dictText'
          },
          {
            title:'成本单价',
            align:"center",
            sorter: true,
            dataIndex: 'stoneUnitCost'
          },
          {
            title:'单价',
            align:"center",
            sorter: true,
            dataIndex: 'stoneUnitPrice'
          },
          {
            title:'批号',
            align:"center",
            sorter: true,
            dataIndex: 'batchNum'
          },
          {
            title:'来单号',
            align:"center",
            sorter: true,
            dataIndex: 'receiveOrderNum'
          },
          {
            title:'主副石',
            align:"center",
            sorter: true,
            dataIndex: 'mainStone_dictText'
          },
          {
            title:'发票类型',
            align:"center",
            sorter: true,
            dataIndex: 'invoiceType_dictText'
          },
          {
            title:'实际币种',
            align:"center",
            sorter: true,
            dataIndex: 'currencyType_dictText'
          },
          {
            title:'实际单价',
            align:"center",
            sorter: true,
            dataIndex: 'stoneUnitPlay'
          },
          {
            title:'实际金额',
            align:"center",
            sorter: true,
            dataIndex: 'stoneCost'
          },
          {
            title:'汇率',
            align:"center",
            sorter: true,
            dataIndex: 'exchangeRate'
          },
          {
            title:'计价方式',
            align:"center",
            sorter: true,
            dataIndex: 'countPricePattern_dictText'
          },
          {
            title:'备注',
            align:"center",
            dataIndex: 'remark'
          },
          {
            title:'发出方',
            align:"center",
            sorter: true,
            dataIndex: 'senderName'
          },
          {
            title:'发出时间',
            align:"center",
            sorter: true,
            dataIndex: 'sendTime'
          },
          {
            title:'接收方',
            align:"center",
            sorter: true,
            dataIndex: 'receiverName'
          },
          {
            title:'接收时间',
            align:"center",
            sorter: true,
            dataIndex: 'receiveTime'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/stoneEntry/list",
          delete: "/stoneEntry/delete",
          deleteBatch: "/stoneEntry/deleteBatch",
          exportXlsUrl: "/stoneEntry/exportXls",
          importExcelUrl: "/stoneEntry/importExcel",

        },
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {
      this.getSuperFieldList();
      this.initColumns();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      opsByStep() {
        this.$refs.stepForm.init()
      },
      //列设置更改事件
      onColSettingsChange (checkedValues) {
        var key = this.$route.name+":colsettings";
        Vue.ls.set(key, checkedValues, 7 * 24 * 60 * 60 * 1000)
        this.settingColumns = checkedValues;
        const cols = this.defColumns.filter(item => {
          if(item.key =='rowIndex'|| item.dataIndex=='action'){
            return true
          }
          if (this.settingColumns.includes(item.dataIndex)) {
            return true
          }
          return false
        })
        this.columns =  cols;
      },
      initColumns(){
        //权限过滤（列权限控制时打开，修改第二个参数为授权码前缀）
        //this.defColumns = colAuthFilter(this.defColumns,'testdemo:');
        var key = this.$route.name+":colsettings";
        let colSettings= Vue.ls.get(key);
        if(colSettings==null||colSettings==undefined){
          let allSettingColumns = [];
          this.defColumns.forEach(function (item,i,array ) {
            allSettingColumns.push(item.dataIndex);
          })
          this.settingColumns = allSettingColumns;
          this.columns = this.defColumns;
        }else{
          this.settingColumns = colSettings;
          const cols = this.defColumns.filter(item => {
            if(item.key =='rowIndex'|| item.dataIndex=='action'){
              return true;
            }
            if (colSettings.includes(item.dataIndex)) {
              return true;
            }
            return false;
          })
          this.columns =  cols;
        }
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'goldActionType',text:'操作类型',dictCode:'gold_action_type'})
        fieldList.push({type:'string',value:'orderCodeNum',text:'订单号',dictCode:'gold_storage,name,id'})
        fieldList.push({type:'string',value:'workNum',text:'工单号',dictCode:'gold_storage,name,id'})
        fieldList.push({type:'sel_search',value:'pn',text:'拆分页',dictTable:'customer_info', dictText:'cus_name', dictCode:'id'})
        fieldList.push({type:'string',value:'styleCodeNum',text:'款号',dictCode:''})
        fieldList.push({type:'sel_search',value:'karat',text:'成色',dictTable:'gold_karat', dictText:'gold_karat_name', dictCode:'id'})
        fieldList.push({type:'string',value:'name',text:'来料名称',dictCode:''})
        fieldList.push({type:'string',value:'cate',text:'种类',dictCode:'gold_type'})
        fieldList.push({type:'string',value:'style',text:'规格',dictCode:''})
        fieldList.push({type:'int',value:'count',text:'数量',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'withStoneWeight',text:'连石重',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'pureStoneWeight',text:'石重(ct)',dictCode:''})
        fieldList.push({type:'string',value:'goldWeight',text:'净重',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'unitPrice',text:'单价（￥）',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'costAmount',text:'价值金额(￥)',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'payAmount',text:'实付金额(￥)',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'attritionRate',text:'耗率(%)',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'giveAttrition',text:'给耗重',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'noAttritionWeight',text:'不计耗重',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'procedureUnitPrice',text:'工费',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'goldContent',text:'折足率(%)',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'goldContentWeight',text:'折足重',dictCode:''})
        fieldList.push({type:'Text',value:'remark',text:'备注',dictCode:''})
        fieldList.push({type:'string',value:'sn',text:'流水单号',dictCode:''})
        fieldList.push({type:'popup',value:'purchaseOrderNum',text:'采购单号', popup:{code:'gold_purchase_report',field:'purchase_order_num',orgFields:'purchase_order_num',destFields:'purchase_order_num'}})
        fieldList.push({type:'sel_user',value:'sender',text:'发出方'})
        fieldList.push({type:'date',value:'sendTime',text:'发出时间'})
        fieldList.push({type:'sel_user',value:'receiver',text:'接收方'})
        fieldList.push({type:'date',value:'receiveTime',text:'接收时间'})
        this.superFieldList = fieldList
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>